﻿using System;
using System.Collections.Generic;
#if DEBUG
using System.Diagnostics;
#endif // #if DEBUG
using System.Linq;
using System.Web;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.WebControls;

using FRL.DebtChased.Website;
using FRL.DebtChased.Repository;
using FRL.DebtChased.Repository.PartModels;

namespace FRL.DebtChased.Website.FinanceParts
{

	/// <summary>
	/// Declaration of FinAccountsReviewedTable class
	/// </summary>
	public class FinAccountsReviewedTable : BaseWebPart
	{

		/////////////////////////////////////////////////////////////
		// Attributes

		Button m_butDebtChaser;

		/////////////////////////////////////////////////////////////
		// Construction

		public FinAccountsReviewedTable()
			:base("Accounts Reviewed")
		{

		}

		/////////////////////////////////////////////////////////////
		// Implementation

		protected override int ContentWidth
		{
			get { return 260; }
		}

		protected override int ContentHeight
		{
			get { return 0; }
		}

		protected override string GrivViewSkinId
		{
			get { return "AccountsReviewedGridView"; }
		}

		protected override void _fillContent(System.Web.UI.HtmlTextWriter w, GridView gridView)
		{

			// Build a table
			w.Write("<table border='0' cellspacing='0' cellpadding='0'");
			w.Write("<tr>");
			w.Write("<td align='center'>");
			gridView.RenderControl(w);
			w.Write("</td>");
			w.Write("</tr>");
			w.Write("<tr>");
			w.Write("<td align='center' valign='center'>");
			m_butDebtChaser.RenderControl(w);
			w.Write("</td>");
			w.Write("</tr>");
			w.Write("</table>");

		}

		protected override void _loadModel()
		{

			// Get repository
			DebtChasedEntities ent = base._getEnt();

			// Load the model
			m_model = new AccountsReviewedModel(ent);

		}

		protected override void _bindGrid(GridView gridView)
		{

			// Shortcuts
			if (gridView.DataSource != null)
				return;
			if (m_model == null)
				return;

			// Set up grid
			gridView.DataKeyNames = new string[1] { "AccountId" };
			//gridView.DataSource = (m_model as AccountsReviewedModel).m_data;
			gridView.DataBind();

		}

		protected override void _processCommand(string strCommandName, int iId)
		{

			// Compare commands...
			if (strCommandName == "DebtChase")
			{

				// Open the debt chaser
				HttpContext.Current.Response.Redirect("DebtChaser.aspx");

			}
#if DEBUG
			else
				Trace.Assert(false);
#endif // #if DEBUG

		}

		protected override void _createControls()
		{

			// Call base-class function
			base._createControls();

			// Create the debt chase button
			m_butDebtChaser = new Button();
			m_butDebtChaser.ID = "butDebtChaser";
			m_butDebtChaser.SkinID = "WebPartsDebtChaseButton";
			m_butDebtChaser.Text = "Debt Chaser";
			m_butDebtChaser.Click += new EventHandler(m_butDebtChaser_Click);
			Controls.Add(m_butDebtChaser);

		}

		void m_butDebtChaser_Click(object sender, EventArgs e)
		{

			// Get my session
			MySession mySession = MySession.BindMySession();

			// Add accounts ids
			mySession.UniqueAddExtraAccountIdsForChaseSession(base.GridView, "checkBoxCol");

			// Redirect to the debt-chaser
			if (mySession.ExtraAccountIdsForChaseSession.Count > 0)
				HttpContext.Current.Response.Redirect("DebtChaser.aspx");

		}

	}

}